package com.jadefashion.controller;

import com.alibaba.fastjson.JSON;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.User;
import com.jadefashion.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    UserService userService;

    @RequestMapping("findUser.do")
    @PreAuthorize("isAuthenticated()")
    private Object findUserName(Authentication authentication) {
        Result result = null;
        try {
            String name = authentication.getName();
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            result = new Result(true, MessageConstant.LOGIN_SUCCESS, name);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(true, MessageConstant.LOGIN_ERROR);
        }
        return result;
    }

    /**
     * 用户数据查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            Map map = userService.queryPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 添加用户信息
     *
     * @param map
     * @return
     */
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody HashMap<String, String> map) {
        try {

            User user = JSON.parseObject(map.get("formData"), User.class);
            List<Integer> roleIds = JSON.parseArray(map.get("roleIds"), Integer.class);

            userService.addUser(roleIds, user);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    /**
     * 删除用户信息
     *
     * @param map
     * @return
     */
    @RequestMapping("/deleteUser")
    public Result deleteUser(@RequestBody HashMap<String, Object> map) {
        try {
            //根据id删除用户并返回Result
            Integer id = (Integer) map.get("id");

            userService.deleteUserById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (RuntimeException e) {
            //捕获抛出用户被引用异常
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    /**
     * 编辑用户信息
     *
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        try {
            userService.updateUser(user);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    /**
     * 更新用户对应角色信息
     *
     * @param map
     * @return
     */
    @RequestMapping("/updateRoleForUserByDetail")
    public Result updateRoleForUserByDetail(@RequestBody HashMap<String, String> map) {
        try {
            //获取删除ids
            List<Integer> deleteIds = JSON.parseArray(map.get("deleteIds"), Integer.class);

            //获取添加ids
            List<Integer> addIds = JSON.parseArray(map.get("addIds"), Integer.class);
            //获取user id
            Integer currentRowId = Integer.parseInt(map.get("currentRowId"));

            userService.updateRoleForUserByDetail(deleteIds, addIds, currentRowId);
            return new Result(true, MessageConstant.UPDATE_ROLE_FOR_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ROLE_FOR_USER_FAIL);
        }
    }

}
